package com.mt.roll.common.service.impl;

import com.mt.roll.common.mapper.CommonMapper;
import com.mt.roll.common.service.CommonService;
import com.mt.roll.common.vo.SysFileVo;
import com.mt.roll.core.config.AliyunSmsConfig;
import com.mt.roll.core.config.CommonConfig;
import com.mt.roll.core.config.TencentCloudConfig;
import com.mt.roll.core.config.TencentCloudSmsConfig;
import com.mt.roll.core.params.IdParam;
import com.mt.roll.core.base.BaseService;
import com.mt.roll.core.vo.R;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;

/**
 * <p>
 *
 * </P>
 *
 * @author FanYi
 * @date 2020/7/6
 * @since 1.0
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class CommonServiceImpl extends BaseService implements CommonService {

    private final CommonMapper mCommonMapper;

    private final CommonConfig mCommonConfig;

    private final AliyunSmsConfig mAliyunSmsConfig;

    private final TencentCloudSmsConfig tencentCloudSmsConfig;

    private final TencentCloudConfig tencentCloudConfig;

    private final CommonConfig commonConfig;



    /**
     * 获取附件下载地址
     *
     * @param fileId 附件ID
     * @return 附件下载地址
     */
    @Override
    public SysFileVo downloadFile(Long fileId) {

        return new SysFileVo().setFileId(null).setFileName(null).setFileUrl(mCommonConfig.getApiHost() + "/common/" + "/home".replace(mCommonConfig.getUploadPath(), ""));
    }


    @Override
    public R<Object> downLoadFileUrl(IdParam idParam, HttpServletResponse response) {

        // 文件扩展名
        String path = null;
        File picFile = new File(path);
        String fileName = picFile.getName();
        try {
            // 文件扩展名
            String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
            URL u = new URL("file:///" + path);
            String contentType = null;

            contentType = u.openConnection().getContentType();

            response.setContentType(contentType);
            response.setHeader("Content-Disposition", "inline;filename=" + new String(("dw." + suffix).getBytes("GBK"), "ISO8859_1"));
            ServletOutputStream outputStream = response.getOutputStream();
            FileInputStream inputStream = new FileInputStream(picFile);
            byte[] buffer = new byte[1024];
            int i = -1;
            while ((i = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, i);
            }
            outputStream.flush();
            outputStream.close();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
            e.printStackTrace();
            log.error("文件预览失败：" + e.getMessage());
            return R.failed("文件预览失败");
        }
        return null;
    }

}
